import { ElMessage, ElMessageBox } from 'element-plus'
import {
    approvedByReview,
    reviewFailed,
    initiatePayment,
    giveJewelry,
    transferFundsToUser
} from '@/api/order'
import router from '@/router'
import type { OrderData } from '@/types/order'

export function useOrderActions(refreshList: () => void) {
    const handleAuditPass = (row: OrderData) => {
        ElMessageBox.confirm('确认审核通过该订单？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }).then(async () => {
            const res = await approvedByReview([row.orderNumber])
            if (res.data.length > 0) {
                ElMessage.success('审核通过')
                refreshList()
            } else {
                ElMessage.error('审核失败')
            }
        })
    }

    const handleAuditReject = (row: OrderData) => {
        ElMessageBox.confirm('确认审核不通过该订单？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }).then(async () => {
            const res = await reviewFailed([row.orderNumber])
            if (res.data.length > 0) {
                ElMessage.success('审核成功')
                refreshList()
            } else {
                ElMessage.error('审核失败')
            }
        })
    }

    const handleCheckInventory = (row: OrderData) => {
        ElMessageBox.confirm('确认检查库存是否到账？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }).then(async () => {
            try {
                const res = await initiatePayment([row.orderNumber])
                if (res.data.length > 0) {
                    ElMessage.success('库存到账成功')
                    refreshList()
                } else {
                    ElMessage.error('库存未到账')
                }
            } catch (error) {
                ElMessage.error('库存检查失败')
            }
        })
    }

    const handleCheckRepayment = (row: OrderData) => {
        ElMessageBox.confirm('确认检查还款是否到账？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }).then(async () => {
            try {
                const res = await giveJewelry([row.orderNumber])
                if (res.data.length > 0) {
                    ElMessage.success('到账成功')
                    refreshList()
                } else {
                    ElMessage.error('未到账')
                }
            } catch (error) {
                ElMessage.error('还款检查失败')
            }
        })
    }

    const handlePayment = (row: OrderData) => {
        ElMessageBox.confirm('确认付款？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }).then(async () => {
            try {
                const res = await transferFundsToUser([row.orderNumber])
                if (res.data.length > 0) {
                    ElMessage.success('付款成功')
                    refreshList()
                } else {
                    ElMessage.error('付款失败')
                }
            } catch (error) {
                ElMessage.error('付款操作失败')
            }
        })
    }

    const viewInformation = (row: OrderData) => {
        router.push({
            path: '/orderDetail',
            query: { ...row }
        })
    }

    return {
        handleAuditPass,
        handleAuditReject,
        handleCheckInventory,
        handleCheckRepayment,
        handlePayment,
        viewInformation
    }
}